package fina2.dcs.system;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.ejb.Remove;
import javax.ejb.Stateless;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.transaction.UserTransaction;

import org.jboss.annotation.ejb.LocalBinding;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Destroy;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.log.Log;

import fina2.dcs.fis.Bank;
import fina2.dcs.fis.BankLocal;

@Stateless
@Name("sysUsrBank")
@Scope(ScopeType.SESSION)
@LocalBinding(jndiBinding = "SysUserBankSession/local")
@AutoCreate
@SuppressWarnings("serial")
@TransactionManagement(TransactionManagementType.BEAN)
public class SysUserBankSession implements SysUserBankLocal, Serializable {

	@PersistenceContext
	private EntityManager em;

	@EJB
	private BankLocal bl;

	@Logger
	private Log log;

	@Resource
	private UserTransaction ut;

	public SysUserBankSession() {

	}

	public long getUsersBanksSize(BigDecimal userId) {
		long banks = 0;
		try {
			long l = System.currentTimeMillis();
			banks = bl.getBanksCount(userId);
			System.out.println("#########" + (System.currentTimeMillis() - l));
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
		}
		return banks;
	}

	public List<Bank> getAllUsersBanks(BigDecimal userId) {
		List<Bank> userBanks = new ArrayList<Bank>();
		String sql = "FROM " + Bank.class.getName() + " b WHERE b.id IN(SELECT sub.userBankPk.bankId FROM " + SysUserBank.class.getName() + " sub WHERE sub.userBankPk.userId=:userId)";
		try {
			long l = System.currentTimeMillis();
			log.info("Getting All Users banks");

			Query q = em.createQuery(sql);
			q.setParameter("userId", userId);
			userBanks = q.getResultList();

			log.info("Users banks loaded");
			log.info("USERS BANKS LOADED IN " + (System.currentTimeMillis() - l));

		} catch (Exception ex) {
			log.error(sql);
			log.error(ex.getMessage(), ex);
		}
		return userBanks;
	}

	public List<String> getAllUsersBankCodes(BigDecimal userId) {
		List<String> userBanks = new ArrayList<String>();
		String sql = "SELECT LTRIM(RTRIM(b.code)) FROM " + Bank.class.getName() + " b WHERE b.id IN(SELECT sub.userBankPk.bankId FROM " + SysUserBank.class.getName() + " sub WHERE sub.userBankPk.userId=:userId  ) ORDER By b.code ASC";
		try {
			ut.begin();
			//log.info("Getting All Users banks codes");

			Query q = em.createQuery(sql);
			q.setParameter("userId", userId);
			userBanks = q.getResultList();
			
			if(userBanks!=null)
				for(int i=0;i<userBanks.size();i++) {
					userBanks.set(i, userBanks.get(i).trim());
				}
			//log.info("Users banks codes loaded");
			ut.commit();
		} catch (Exception ex) {
			log.error(sql);
			log.error(ex.getMessage(), ex);
		}
		return userBanks;
	}

	@Remove
	public void remove() {

	}
}
